package cn.juque.redoctopus.convert;

import cn.hutool.core.util.ArrayUtil;
import cn.juque.redoctopus.bo.field.FieldDocBO;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.lucene.search.FieldDoc;

/**
 * @author nuoka
 * @version 1.0.0
 * <li></li>
 * @date 2021/11/13 18:59
 **/
public class FieldDocConverter {

    public static FieldDoc toFieldDoc(FieldDocBO bo) {
        if (bo == null) {
            return null;
        }
        return new FieldDoc(bo.getDoc(), bo.getScore(), ArrayUtil.toArray(bo.getFields(), Long.class));
    }

    /**
     * 转换 FieldDocBO
     * @param fieldDoc FieldDoc
     * @return FieldDocBO
     */
    public static FieldDocBO toFieldDocBO(FieldDoc fieldDoc) {
        if (fieldDoc == null) {
            return null;
        }
        List<Long> fields = Arrays.stream(fieldDoc.fields).map(t->(Long)t).collect(Collectors.toList());
        FieldDocBO fieldDocBO = new FieldDocBO();
        fieldDocBO.setFields(fields);
        fieldDocBO.setScore(fieldDoc.score);
        fieldDocBO.setDoc(fieldDoc.doc);
        fieldDocBO.setShardIndex(fieldDoc.shardIndex);
        return fieldDocBO;
    }
}
